package toledo.rest;

import java.util.List;
import java.util.Vector;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.FilterOperator;

@Path("/entry")
public class Entry
{
	public static final String DATABASE = "entries";
	public static final String COURSE_ID = "course_id";
	public static final String ENTRY_ID = "entry_id";
	public static final String USER_ID = "user_id";
	public static final String DATE = "date";
	public static final String NUM_REPLIES = "num_replies";
	public static final String TEXT = "text";
	public static final String REPLY_TO_ID = "reply_to_id";
	public static final String TYPE = "type";
	public static final String KEY = Entity.KEY_RESERVED_PROPERTY;
	
	public static final String TYPE_COMMENT = "comment";
	public static final String TYPE_TWEET = "tweet";
	public static final String TYPE_POST = "post";

	public static Entity getEntry(Object entry_id)
	{
		try
		{
			return DatastoreServiceFactory.getDatastoreService().get(KeyFactory.stringToKey(entry_id.toString()));
		}
		catch(EntityNotFoundException e)
		{
			
		}
		return null;
	}
	public static List<Entity> getComments(Object reply_to_id)
	{
		return DatastoreServiceFactory.getDatastoreService().prepare(new Query(Entry.DATABASE).addFilter(Entry.REPLY_TO_ID,FilterOperator.EQUAL,reply_to_id)).asList(FetchOptions.Builder.withDefaults());
	}
	
	@Path("/comments")
	@GET
	@Produces("application/json")
	public String getEntries(@QueryParam(Entry.ENTRY_ID)String entry_id)
	{
		DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
		JSONObject json = new JSONObject();
		Vector<JSONObject> list = new Vector<JSONObject>();
		
		List<Entity> entries = ds.prepare(new Query(Entry.DATABASE).addFilter(Entry.REPLY_TO_ID,FilterOperator.EQUAL,entry_id)).asList(FetchOptions.Builder.withDefaults());
		for(Entity entry:entries)
		{
			try
			{
				JSONObject o = new JSONObject();
				Entity user = User.getUser(entry.getProperty(Entry.USER_ID));
				o.put(User.FULLNAME,user.getProperty(User.FULLNAME));
				o.put(Entry.ENTRY_ID,KeyFactory.keyToString(entry.getKey()));
				o.put(User.PIC_URL,user.getProperty(User.PIC_URL));
				o.put(Entry.DATE,entry.getProperty(Entry.DATE));
				o.put(Entry.TEXT,entry.getProperty(Entry.TEXT));
				list.add(o);
			}
			catch(JSONException e)
			{
			}
		}
		
		JSONArray results = new JSONArray(list);
		try
		{
			Entity entry = Entry.getEntry(entry_id);
			Entity user = User.getUser(entry.getProperty(Entry.USER_ID));
			Entity course = Course.getCourse(entry.getProperty(Entry.COURSE_ID));
			//json.put("total_results", results.length());
			json.put(Entry.TEXT,entry.getProperty(Entry.TEXT));
			json.put(Entry.DATE,entry.getProperty(Entry.DATE));
			json.put(User.FULLNAME,user.getProperty(User.FULLNAME));
			json.put(User.PIC_URL,user.getProperty(User.PIC_URL));
			json.put(Course.COURSENAME,course.getProperty(Course.COURSENAME));
			//json.put(Course.HASHTAG,course.getProperty(Course.HASHTAG));
			json.put("results", results);
		}
		catch (JSONException e)
		{
		}
		
		return json.toString();
	}
}